package com.fire.control.system.support.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.fire.control.system.dto.ResourceDTO;
import com.fire.control.system.dto.ResourceThreeDTO;
import com.fire.control.system.entity.Resource;
import com.fire.control.system.entity.Role;

public class ResourceTreeUtil {
	
	public static List<ResourceDTO> getRoleResources(Collection<Role> roles){
		Set<Resource> resourceSet = new HashSet<Resource>(); 
		for(Role role : roles){
			Collection<Resource> resourceCon = role.getResources();
			if(resourceCon != null && resourceCon.isEmpty() == false){
				resourceSet.addAll(resourceCon);
			}
		}
		return resourceTree(resourceSet);
	}
	
	
	public static List<ResourceDTO> resourceTree(Set<Resource> resourceSet){
		List<ResourceDTO> firstNodeList = new ArrayList<ResourceDTO>();
		if(resourceSet!=null && !resourceSet.isEmpty()){			
			for(Resource resource : resourceSet){
				if(resource.getResourceCode().length() == BusinessStaticValue.first_node_code_length){
					ResourceDTO dto = new ResourceDTO();
					dto.setResource(resource);
					dto.setId(resource.getId());
					dto.setName(resource.getName());
					dto.setResourceCode(resource.getResourceCode());
					dto.setResourceType(resource.getResourceType());
					dto.setResourceUrl(resource.getResourceUrl());
					dto.setResourceDesc(resource.getResourceDesc());
					dto.setPath(resource.getPath());
					dto.setSort(resource.getSort());
					List<ResourceDTO> children = getResourceChildren(resourceSet, dto);
					dto.setChildren(children);
					firstNodeList.add(dto);
				}
			}
		}
		ListSortUtil<ResourceDTO> msList = new ListSortUtil<ResourceDTO>();
        msList.sortByMethod(firstNodeList, "getSort", false);
		return firstNodeList;
	}
	
	public static List<ResourceDTO> getResourceChildren(Set<Resource> resourceSet,ResourceDTO parentNode){
		List<ResourceDTO> childNodeList = new ArrayList<ResourceDTO>();
		String parentResourceCode = parentNode.getResourceCode();
		for(Resource resource : resourceSet){
			String childResourceCode = resource.getResourceCode();
			if(childResourceCode.startsWith(parentResourceCode) && (childResourceCode.length() - parentResourceCode.length()) == BusinessStaticValue.parent_and_child_length_difference){
				ResourceDTO dto = new ResourceDTO();
				dto.setResource(resource);
				dto.setId(resource.getId());
				dto.setName(resource.getName());
				dto.setResourceCode(resource.getResourceCode());
				dto.setResourceType(resource.getResourceType());
				dto.setResourceUrl(resource.getResourceUrl());
				dto.setResourceDesc(resource.getResourceDesc());
				dto.setPath(resource.getPath());
				dto.setSort(resource.getSort());
				dto.setChildren(getResourceChildren(resourceSet, dto));
				childNodeList.add(dto);
			}
		}
		ListSortUtil<ResourceDTO> msList = new ListSortUtil<ResourceDTO>();
        msList.sortByMethod(childNodeList, "getSort", false);
		return childNodeList;
	}
	
	
	/********************************************ZTree Json数据返回**************************************************************/
	public static List<ResourceThreeDTO> getResourceThree(Collection<Role> roles){
		Set<Resource> resourceSet = new HashSet<Resource>(); 
		for(Role role : roles){
			Collection<Resource> resourceCon = role.getResources();
			if(resourceCon != null && resourceCon.isEmpty() == false){
				resourceSet.addAll(resourceCon);
			}
		}
		return resourceDTOTree(resourceSet);
	}
	
	
	public static List<ResourceThreeDTO> resourceDTOTree(Set<Resource> resourceSet){
		List<ResourceThreeDTO> firstNode = new ArrayList<ResourceThreeDTO>();
		for(Resource resource : resourceSet){
			if(resource.getResourceCode().length() == BusinessStaticValue.first_node_code_length){
				ResourceThreeDTO dto = new ResourceThreeDTO();
				dto.setId(resource.getId());
				dto.setName(resource.getName());
				dto.setResourceCode(resource.getResourceCode());
				dto.setResourceType(resource.getResourceType());
				dto.setResourceUrl(resource.getResourceUrl());
				dto.setResourceDesc(resource.getResourceDesc());
				dto.setPath(resource.getPath());
				dto.setChildren(getResourceThreeChildren(resourceSet, dto));
				firstNode.add(dto);
			}
		}
		
		return firstNode;
	}
	
	public static List<ResourceThreeDTO> getResourceThreeChildren(Set<Resource> resourceSet,ResourceThreeDTO parentNode){
		List<ResourceThreeDTO> result = new ArrayList<ResourceThreeDTO>();
		String parentResourceCode = parentNode.getResourceCode();
		for(Resource resource : resourceSet){
			String childResourceCode = resource.getResourceCode();
			if(childResourceCode.startsWith(parentResourceCode) && (childResourceCode.length() - parentResourceCode.length()) == BusinessStaticValue.parent_and_child_length_difference){
				parentNode.setOpen(true);
				ResourceThreeDTO dto = new ResourceThreeDTO();
				dto.setId(resource.getId());
				dto.setName(resource.getName());
				dto.setResourceCode(resource.getResourceCode());
				dto.setResourceType(resource.getResourceType());
				dto.setResourceUrl(resource.getResourceUrl());
				dto.setResourceDesc(resource.getResourceDesc());
				dto.setPath(resource.getPath());
				dto.setChildren(getResourceThreeChildren(resourceSet, dto));
				result.add(dto);
			}
		}
		return result;
	}
}
